import ChannelManager from "../manager/ChannelManager";

const { ccclass } = cc._decorator;

@ccclass
export default class AdsSimulator extends cc.Component {

    //#region 广告相关
    //广告面板
    private ads: cc.Node;
    //广告图片
    private ads_image: cc.Sprite;
    //广告加载
    private loading: cc.Node;
    //关闭广告按钮
    private ads_close: cc.Node;
    //广告时间提示
    private ads_timer: cc.Node;
    //广告时间提示文本
    private ads_timer_label: cc.Label;
    //广告计时器
    private ads_reward_time: number;
    //广告成功回调
    private on_ads_success: Function;
    //广告失败回调
    private on_ads_fail: Function;
    //广告回调定义域
    private ads_banner: cc.Node;
    private ads_banner_icon: cc.Sprite;
    private ads_banner_title: cc.Label;
    private ads_banner_content: cc.Label;
    private ads_banner_play: cc.Node;
    private ads_banner_play_label: cc.Label;
    private ads_banner_share: cc.Node;
    private ads_banner_share_label: cc.Label;

    private taget_app_id: string;
    //#endregion 广告相关
    /**
     * 初始化广告
     */
    public initAds() {
        this.ads = this.node.getChildByName("ads");
        this.loading = this.ads.getChildByName("loading");
        this.ads_close = this.ads.getChildByName("close");
        this.ads_timer = this.ads.getChildByName("toast");
        this.ads_timer_label = this.ads_timer.getChildByName("label").getComponent(cc.Label);
        this.ads_image = this.ads.getChildByName("image").getComponent(cc.Sprite);

        this.ads_banner = this.ads.getChildByName("banner");
        this.ads_banner_icon = this.ads_banner.getChildByName("icon").getComponent(cc.Sprite);
        this.ads_banner_title = this.ads_banner.getChildByName("title").getComponent(cc.Label);
        this.ads_banner_content = this.ads_banner.getChildByName("content").getComponent(cc.Label);
        this.ads_banner_play = this.ads_banner.getChildByName("btn_play");
        this.ads_banner_play_label = this.ads_banner_play.getChildByName("label").getComponent(cc.Label);
        this.ads_banner_share = this.ads_banner.getChildByName("btn_share");
        this.ads_banner_share_label = this.ads_banner_share.getChildByName("label").getComponent(cc.Label);
        let systemInfo = ChannelManager.GetInstance().GetSystemInfo();
        if (systemInfo) {
            let local = systemInfo.safeArea.top * systemInfo.pixelRatio;
            this.ads_timer.getComponent(cc.Widget).top = local - 32;
            this.ads_close.getComponent(cc.Widget).top = local - 32;
        }

        this.ads_close.on(cc.Node.EventType.TOUCH_END, this.OnCloseAdsClick, this);
        this.ads_banner_play.on(cc.Node.EventType.TOUCH_END, this.OnJumpGame, this);
        this.ads_banner_share.on(cc.Node.EventType.TOUCH_END, this.OnShareGame, this);
    }

    /**
     * 播放广告
     * @param on_success 成功回调
     * @param on_failed 失败回调
     * @param caller 定义域
     */
    public ShowAds(on_success: Function, on_failed: Function) {
        this.on_ads_success = on_success;
        this.on_ads_fail = on_failed;
        this.node.active = true;
        this.ads_image.node.active = false;
        this.ads_banner.active = false;

        this.ads_close.active = false;
        this.ads_timer.active = true;
        this.ads_timer_label.string = "15秒后可获得奖励";
        this.ads_reward_time = 15;
        console.log(">>>>>>>>>>>>>>>>", 2);
        this.loading.active = true;

        cc.loader.loadRes("ads/config", function (err, data) {
            let ads_data = data.json[this.Random(0, data.json.length - 1)];
            cc.loader.load(ads_data.icon_path, function (err, data) {
                if (err) {
                    return;
                }
                this.ads_banner_icon.spriteFrame = new cc.SpriteFrame(data);
                this.ads_banner_title.string = ads_data.title;
                this.ads_banner_content.string = ads_data.content;

                if (ads_data.self) {
                    this.ads_banner_share.active = true;
                    this.ads_banner_share_label.string = ads_data.button;
                } else {
                    this.taget_app_id = ads_data.app;
                    this.ads_banner_play.active = true;
                    this.ads_banner_share.active = false;
                    this.ads_banner_play_label.string = ads_data.button;
                }

                this.ads_banner.active = true;
                this.schedule(this.ShowAdsTimer, 1);
            }.bind(this));

            let path = cc.view.getFrameSize().width < cc.view.getFrameSize().height ? ads_data.l_img_path : ads_data.v_img_path;
            cc.loader.load(path, function (err, data) {
                if (!err) {
                    this.loading.active = false;
                    this.ads_image.node.active = true;
                    this.ads_image.spriteFrame = new cc.SpriteFrame(data);
                }
                this.schedule(this.ShowAdsTimer, 1);
            }.bind(this));
        }.bind(this))
    }

    /**
     * 广告播放计时器
     */
    private ShowAdsTimer() {
        this.ads_reward_time--;
        this.ads_timer_label.string = this.ads_reward_time + "秒后可获得奖励";
        if (this.ads_reward_time <= 0) {
            this.unschedule(this.ShowAdsTimer);
            this.ads_close.active = true;
            this.ads_timer.active = false;
        }
    }

    /**
     * 当点击关闭广告按键
     */
    private OnCloseAdsClick() {
        this.node.active = false;
        if (this.on_ads_success) this.on_ads_success();
    }

    /**
     * 跳转游戏
     */
    private OnJumpGame() {
        ChannelManager.GetInstance().NavigateToMiniProgram(this.taget_app_id, "", { ads: 1 }, null, null, false);
    }

    /**
     * 分享游戏
     */
    private OnShareGame() {
        ChannelManager.GetInstance().ShareGame();
    }

    /**
     * 随机数
     * @param min 最小值
     * @param max 最大值
     */
    public Random(min: number, max: number): number {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
}
